/* TODO: MapFactory 
 * TODO: add State
 * TODO: modifty Config.js
 * TODO: add MapFactory
 *  NOTE: test only firefox 
 *  SOUND: by maoudamashii
 */
/* Omajinai*/	
enchant();

/* global var */
var g_game;
var enemies = new Array();
var g_cannons = new Array();
var g_gold    = 5;
var g_life = 10;
var goldLabel;

/* State */

//敵が曲がるポイント
var roadPoints = [
[4*16-8, 17*16],
	[4*16-8, 15*16],
	[1*16-8, 15*16],
	[1*16-8, 12*16],
	[4*16-8, 12*16],
	[4*16-8,  8*16],
	];


/* Management Waves */
var WaveManager = enchant.Class.create(enchant.Sprite, {

		});

/* Wave */
var Wave = enchant.Class.create(enchant.Sprite, {
initialize:function(type, hp, money){
this.enemyType = type;
this.enemyHp = hp;
this.enemyMoney = money;	
},

});
/* Menu */





/* initialize */
function initialize(){

	State.setGold(5);

};

function update(){
	/* update enemy  */
	if(g_game.frame % Conf.getWaveInterVal() == 0){
		var enemy = new Enemy(4*16-8, 17*16);
	}
	/* Game Over */
	/*goldLabel.text = "Cannon:" + (g_cannons.length-1);*/
	/*lifeLabel.text = "Life:" + g_life;*/
	/* Game Over*/
	if(g_life <= 0){
		g_game.end(g_gold, "Your Score is " + g_gold);
		SoundFactory.getInstance().play('ending', true);

	}

};

function main(){
	g_game = new Game(Conf.getGameWidth(), Conf.getGameHeight());
	g_game.fps = Conf.getFrameRate();
	g_game.score = Conf.getInitialScore();

	//preload external assets 
	var imagePaths = ImageFactory.getExternalImagePaths();
	var preLoader = new Preloader(imagePaths);
	preLoader.preload(g_game);
	
	initialize();

};

/* game starts*/
window.onload = function() {

	main();


	g_game.onload = function() {
		g_game.touched = false;
		SoundFactory.getInstance().play('opening',true);
		/* MAP */
		var map = new Map(Conf.getMapWidth(), Conf.getMapHeight());
		map.image = g_game.assets[ImageFactory.getExternalImagePath('map1')];
		map.loadData(
				/* field data*/
				Conf.getMapLoadDataField(),

				/* building */
				Conf.getMapLoadDataBuildingAndItem()
			    );
		map.collisionData = Conf.getMapCollisionData();

		/* FOREGROUNDMAP */
		var foregroundMap = new Map(Conf.getMapWidth(), Conf.getMapHeight());
		foregroundMap.image = g_game.assets[ImageFactory.getExternalImagePath('map1')];
		foregroundMap.loadData(Conf.getForeGrdMapLoadData());


		/* stage */
		var stage = new Group();
		stage.addChild(map);
		stage.addChild(foregroundMap);
		g_game.rootScene.addChild(stage);

		/* FrontEnd */	
		var frontEnd = new FrontEnd();
		frontEnd.createGameStatusWindow(0,0);
		g_game.rootScene.addChild(frontEnd);


		/* cannon */
		while(g_gold>0){
			var canon = new Canon(24, -6);
			g_gold -= canon.cost;
			g_cannons.push(canon);
			frontEnd.addChild(canon);
		}

		g_game.rootScene.addEventListener('enterframe', function(){
				update();
		});

	};
	g_game.start();
};
